package com.ja.openchat.ui.chat.redpacket;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.BarUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.ja.openchat.AppViewModelFactory;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.ActivityRedPacketDetailBinding;
import com.ja.openchat.ui.me.WalletFragment;
import com.ja.openchat.viewmodels.RedPacketDetailViewModel;
import com.zy.openchat.core.model.redpacket.RedPacketInfo;
import com.zy.openchat.core.model.user.UserInfo;

import me.goldze.mvvmhabit.base.BaseActivity;

public class RedPacketDetailActivity extends BaseActivity<ActivityRedPacketDetailBinding,RedPacketDetailViewModel> {

    private TextView red_sender,red_title,red_money,wallet_btn,group_content,red_money_lable;
    private View center_line,list_line;
    private RedPacketDetailAdapter mAdapter;
    private RedPacketInfo mRedPacketInfo;


    public static void startActivity(Context context, RedPacketInfo packetInfo){
        Intent intent = new Intent(context,RedPacketDetailActivity.class);
        intent.putExtra("packetInfo", packetInfo);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        super.initView();
        if(getIntent()!=null){
            mRedPacketInfo = getIntent().getParcelableExtra("packetInfo");
        }

        binding.backBtn.setOnClickListener(v -> finish());
        mAdapter = new RedPacketDetailAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(mAdapter);
        View view = LayoutInflater.from(this).inflate(R.layout.view_red_packet_detail_header,binding.recyclerView,false);
        initHeaderView(view);
        mAdapter.addHeaderView(view);
        ((ViewGroup.MarginLayoutParams)binding.backBtn.getLayoutParams()).topMargin = ImmersionBar.getStatusBarHeight(this);


        if(mRedPacketInfo!=null){
            red_sender.setText(String.format("%s发出的现金红包", UserInfo.getUserInfo(mRedPacketInfo.from).displayName()));
            red_title.setText(mRedPacketInfo.title);
            float mePrice = mRedPacketInfo.curUserRpPrice();
            if(mePrice == 0.00f){
                red_money.setVisibility(View.GONE);
                red_money_lable.setVisibility(View.GONE);
                wallet_btn.setVisibility(View.GONE);
            }else{
                wallet_btn.setVisibility(View.VISIBLE);
                red_money.setVisibility(View.VISIBLE);
                red_money_lable.setVisibility(View.VISIBLE);
                red_money.setText(String.valueOf(mRedPacketInfo.curUserRpPrice()));
            }
            if(mRedPacketInfo.type == 1){
                center_line.setVisibility(View.INVISIBLE);
                group_content.setVisibility(View.VISIBLE);
                list_line.setVisibility(View.GONE);
                group_content.setText(mRedPacketInfo.detailDes());
            }else{
                center_line.setVisibility(View.VISIBLE);
                group_content.setVisibility(View.VISIBLE);
                list_line.setVisibility(View.VISIBLE);
                group_content.setText(mRedPacketInfo.detailDes());
                if(mRedPacketInfo.isDone()){
                    mAdapter.setBestUserId(mRedPacketInfo.bestUserId());
                }
            }
            mAdapter.setNewInstance(mRedPacketInfo.users);
        }
    }

    private void initHeaderView(View view){
        red_sender = view.findViewById(R.id.red_sender);
        red_title = view.findViewById(R.id.red_title);
        red_money = view.findViewById(R.id.red_money);
        wallet_btn = view.findViewById(R.id.wallet_btn);
        center_line = view.findViewById(R.id.center_line);
        group_content = view.findViewById(R.id.group_content);
        list_line = view.findViewById(R.id.list_line);
        red_money_lable = view.findViewById(R.id.red_money_lable);

        wallet_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startContainerActivity(WalletFragment.class.getCanonicalName());
            }
        });
    }

    @Override
    public void setStatusBar() {
        BarUtils.setStatusBarLightMode(this, false);
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
//        StatusBarUtil.setMargin(this, findViewById(R.id.toolbar));
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_red_packet_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public RedPacketDetailViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(RedPacketDetailViewModel.class);
    }
}
